﻿using System;
using Microsoft.Win32;
using System.Runtime.InteropServices;

namespace TogNet.BusinessObjects
{
    public class ProxyChanger
    {
        public static void EnableProxy(bool enable)
        {
            using (var key = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Internet Settings", true))
            {
                if (key != null) 
                    key.SetValue("ProxyEnable", Convert.ToInt32(enable), RegistryValueKind.DWord);

                InternetSetOption(IntPtr.Zero, INTERNET_OPTION_SETTINGS_CHANGED, IntPtr.Zero, 0);
                InternetSetOption(IntPtr.Zero, INTERNET_OPTION_REFRESH, IntPtr.Zero, 0);
            }
        }

        [DllImport("wininet.dll")]
        public static extern bool InternetSetOption(IntPtr hInternet, int dwOption, IntPtr lpBuffer, int dwBufferLength);
        public const int INTERNET_OPTION_SETTINGS_CHANGED = 39;
        public const int INTERNET_OPTION_REFRESH = 37;
    }

     
}
